package com.lb.bs.client.processor.handle.dynamics.update;

import com.alibaba.fastjson.JSON;
import com.lb.bs.client.annotation.SingletonMark;
import com.lb.bs.client.callback.UpdateCallBackService;
import com.lb.bs.client.repository.StoreCenter;
import com.lb.bs.client.processor.ProcessorHandleService;
import com.lb.bs.client.processor.beans.ProcessorDynamicHandlerBean;
import com.lb.bs.client.util.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * program: LB-BS-Client
 * author: bsworld.xie
 * create: 2019-12-06 11:19
 * description:
 */
@SingletonMark
public class UpdateCallBackProcessorHandle implements ProcessorHandleService<ProcessorDynamicHandlerBean> {
    private static final Logger logger = LoggerFactory.getLogger(UpdateCallBackProcessorHandle.class);
    @Override
    public void handleProcess(ProcessorDynamicHandlerBean handlerBean) {
        logger.info("UpdateCallBackProcessorHandle(), handlerBean:{}", JSON.toJSONString(handlerBean));
        StoreCenter storeCenter = StoreCenter.getInstance();
        String key = handlerBean.getKey();
        Map<String, List<UpdateCallBackService>> itemKeyCallBackMap = storeCenter.getItemKeyCallBackMap();
        if (CollectionUtils.isEmpty(itemKeyCallBackMap)) {
            return;
        }
        List<UpdateCallBackService> updateCallBackServiceList = itemKeyCallBackMap.get(key);
        if (CollectionUtils.isEmpty(updateCallBackServiceList)) {
            return;
        }
        boolean enableUpdateCallBack = handlerBean.isEnableUpdateCallBack();
        if (logger.isWarnEnabled()) {
            logger.warn("UpdateCallBackProcessorHandle.class, handlerBean:{},enableUpdateCallBack:{}", JSON.toJSONString(handlerBean), enableUpdateCallBack);
        }
        if (!enableUpdateCallBack) {
            return;
        }

        for (UpdateCallBackService updateCallBackService : updateCallBackServiceList) {
            updateCallBackService.reloadOnKey(handlerBean);
        }
    }
}
